﻿package app.touchMagnify{
	import flash.display.*;
	import flash.text.*;
	import flash.events.Event;
	import flash.xml.*;
	import flash.net.*;
	import flash.events.TUIO;
	import flash.events.TouchEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.utils.Dictionary;
	import app.touchMagnify.ZoomBox;

	public class Magnify extends MovieClip {

		public var shrinkFactor:Number=1/3;
		public var magFactor:Number=1/shrinkFactor;

		public var scaledMap:cleImage =  new cleImage();

		private var activeZooms = new Dictionary(true);
		private var activeBoxes = new Dictionary(true);
		public var Zoomz:MovieClip = new MovieClip();

		public function Magnify():void {
			//--------connect to TUIO-----------------
			TUIO.init(this,'127.0.0.1',3000,'',false);
			trace("AppControl - TUIO Started");
			//----------------------------------------

			addChild(scaledMap);
			scaledMap.x=0;
			scaledMap.y=0;
			scaledMap.scaleX=shrinkFactor;
			scaledMap.scaleY=shrinkFactor;
			scaledMap.addEventListener(TouchEvent.MOUSE_DOWN,boardDown);

			addChild(Zoomz);

		}
		function boardDown(e:TouchEvent):void {
			var intX = e.stageX;
			var intY = e.stageY;
			var zoomMap:cleImage =  new cleImage();
			var newBox:ZoomBox = new ZoomBox(intX, intY);
			newBox.thisname = e.ID.toString();

			Zoomz.addChild(zoomMap);
			Zoomz.addChild(newBox);

			activeZooms[e.ID.toString()] = zoomMap;
			activeBoxes[e.ID.toString()] = newBox;
			activeZooms[newBox.thisname].x = (e.stageX * -2);
			activeZooms[newBox.thisname].y = (e.stageY * -2);

			zoomMap.mask = newBox.maskArea;

			newBox.hitAreaMc.addEventListener(TouchEvent.MOUSE_MOVE,positionZoom);
			newBox.ItemCloseBtn.addEventListener(TouchEvent.MOUSE_DOWN,closeZoom);

		}
		public function positionZoom(e:TouchEvent):void {
			var newItm = e.relatedObject.parent.parent;
			activeZooms[newItm.thisname].x = (newItm.x * -2);
			activeZooms[newItm.thisname].y = (newItm.y * -2);
			if (e.ID.toString() == newItm.thisname) {
				newItm.x = e.stageX;
				newItm.y = e.stageY;
			}
		}

		public function closeZoom(e:TouchEvent):void {
			var oldItm = e.relatedObject.parent.parent;
			oldItm.hitAreaMc.removeEventListener(TouchEvent.MOUSE_MOVE,positionZoom);
			oldItm.ItemCloseBtn.removeEventListener(TouchEvent.MOUSE_DOWN,closeZoom);
			Zoomz.removeChild(activeZooms[oldItm.thisname]);
			Zoomz.removeChild(activeBoxes[oldItm.thisname]);
			e.stopPropagation();
		}

	}
}